﻿using System;
using System.Windows.Controls;
using GooNews.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using GalaSoft.MvvmLight;
using AgiliTrain.PhoneyTools;
using System.Diagnostics;

namespace GooNews
{
  /// <summary>
  /// 
  /// </summary>
  public partial class MainPage : PhoneApplicationPage
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="MainPage"/> class.
    /// </summary>
    public MainPage()
    {
      InitializeComponent();

      // Set the data context of the listbox control to the sample data
      if (!ViewModelBase.IsInDesignModeStatic)
      {
        DataContext = App.ViewModel;
      }

      pivot.Loaded += new System.Windows.RoutedEventHandler(pivot_Loaded);
    }

    /// <summary>
    /// Handles the Loaded event of the pivot control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    void pivot_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
      if (App.ViewModel.IsDirectlyFromActivate())
      {
        var state = PhoneApplicationService.Current.State;

        if (state.ContainsKey("SelectedPivotIndex"))
        {
          int index = (int)state["SelectedPivotIndex"];
          if (index < pivot.Items.Count)
          {
            pivot.SelectedIndex = index;
          }
          state.Remove("SelectedPivotIndex");
        }
      }
    }

    /// <summary>
    /// Handles the Click event of the settingsMenuItem control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void settingsMenuItem_Click(object sender, EventArgs e)
    {
      NavigationService.Navigate(new Uri("/Pages/OptionsPage.xaml", UriKind.Relative));
    }

    /// <summary>
    /// Handles the Click event of the rateMenuItem control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void rateMenuItem_Click(object sender, EventArgs e)
    {
      var task = new MarketplaceReviewTask();
      task.Show();
    }

    /// <summary>
    /// Handles the Click event of the aboutMenuItem control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void aboutMenuItem_Click(object sender, EventArgs e)
    {
      NavigationService.Navigate(new Uri("/Pages/AboutPage.xaml", UriKind.Relative));
    }

    /// <summary>
    /// Handles the Click event of the refreshButton control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void refreshButton_Click(object sender, EventArgs e)
    {
      App.ViewModel.ReloadFeeds();
    }

    /// <summary>
    /// Called when a page is no longer the active page in a frame.
    /// </summary>
    /// <param name="e">An object that contains the event data.</param>
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
      var state = PhoneApplicationService.Current.State;

      if (state.ContainsKey("SelectedPivotIndex"))
      {
        state.Remove("SelectedPivotIndex");
      }

      state.Add("SelectedPivotIndex", pivot.SelectedIndex);
    }

    /// <summary>
    /// Called when a page becomes the active page in a frame.
    /// </summary>
    /// <param name="e">An object that contains the event data.</param>
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
      ApplicationBar.IsVisible = true;
      base.OnNavigatedTo(e);
    }

    /// <summary>
    /// Handles the Click event of the smallerFontIconButton control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void smallerFontIconButton_Click(object sender, EventArgs e)
    {
      App.ViewModel.Options.ChangeFontSize(false);
      App.ViewModel.Options.Save();
    }

    /// <summary>
    /// Handles the Click event of the largerFontIconButton control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void largerFontIconButton_Click(object sender, EventArgs e)
    {
      App.ViewModel.Options.ChangeFontSize(true);
      App.ViewModel.Options.Save();
    }

    /// <summary>
    /// Handles the Click event of the favFeedIconButton control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void favFeedIconButton_Click(object sender, System.EventArgs e)
    {
      App.ViewModel.Options.FavoriteFeed = ((Feed)pivot.SelectedItem).Url;
      App.ViewModel.Options.Save();
      App.ThisApp.ShowMessage("Favorite Feed Saved");
    }



  }
}